import Vue from 'vue';
import Main from './dialog.vue';
const defaults = {
  message: "",
  state: false,
  button: {}
};
let dialogVueLoading;
const DialogConstructor = Vue.extend(Main);
DialogConstructor.prototype.close = function() {
    var vm=this;
    dialogVueLoading=null;
    var promise=new Promise(function(resolve,reject){
            if (vm.$el && vm.$el.parentNode) {
                vm.$el.parentNode.removeChild(vm.$el);
            }
            resolve();
            vm.$destroy();
            vm.state = false;
    })
    return promise
};
const Dialog = (options = {}) => {
    if (Vue.prototype.$isServer) return;
    options = Object.assign({}, defaults, options);
    let parent = document.body ;
    if(dialogVueLoading){
      return dialogVueLoading
    }
    let instance = new DialogConstructor({
      el: document.createElement('div'),
      data: options
    });
  
    parent.appendChild(instance.$el);
    Vue.nextTick(() => {
      instance.state = true;
    });

    dialogVueLoading=instance
    return instance;
  };

  Vue.prototype.$dialog = Dialog
  export default Dialog